<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>全选例子</title>
	</head>
	<body>
			<table border="1" cellspacing="0" cellpadding="1" id="tableid">
					<tr>
						<th></th>
						<th>姓名</th>
						<th>性别</th>
						<th>年龄</th>
						<th>家庭住址</th>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="1" /></td>
						<td>张三发</td>
						<td>男</td>
						<td>19</td>
						<td>福建省龙岩市</td>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="2" /></td>
						<td>张子枫</td>
						<td>女</td>
						<td>21</td>
						<td>北京市朝阳区</td>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="3" /></td>
						<td>藤原拓海</td>
						<td>男</td>
						<td>45</td>
						<td>日本群马县</td>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="4" /></td>
						<td>李洪基</td>
						<td>男</td>
						<td>31</td>
						<td>韩国</td>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="5" /></td>
						<td>詹姆斯</td>
						<td>男</td>
						<td>31</td>
						<td>美国</td>
					</tr>
					<tr>
						<td><input type="checkbox" name="stu" id="" value="6" /></td>
						<td>洪世贤</td>
						<td>男</td>
						<td>31</td>
						<td>中国</td>
					</tr>
			</table>
			<td><input type="checkbox" name="" id="checkAllID" value="" onclick="checkAll();"/></td>全选
			<td><input type="checkbox" name="" id="checkAllIDmen" value="" onclick="checkAllmen();"/></td>全选男
			<td><input type="checkbox" name="" id="checkAllIDwomen" value="" onclick="checkAllwomen();"/></td>全选女
			<button type="submit">提交</button>
	</body>
	<script type="text/javascript">
		function checkAll(){
			var tableobj = document.getElementById('tableid');
			var allinput = tableobj.getElementsByTagName('input');
			var checkAllIDvalue = document.getElementById('checkAllID');
			for(var i =0;i<allinput.length;i++){
				allinput[i].checked = checkAllIDvalue.checked;
			}
			checkAllIDmen.checked=false;
			checkAllIDwomen.checked=false;
		}
		function checkAllmen(){
			var sex = document.getElementById('tableid');
			var meninput = document.getElementsByTagName('input');
		for(var i=1;i<tableid.rows.length;i++){
			if(sex.rows[i].cells[2].innerHTML!=='女'){
				meninput[i-1].checked = checkAllIDmen.checked;
				}
			}
			checkAllID.checked=false;
			checkAllIDwomen.checked=false;
		}
		function checkAllwomen(){
			var sex = document.getElementById('tableid');
			var meninput = document.getElementsByTagName('input');
		for(var i=1;i<tableid.rows.length;i++){
			if(sex.rows[i].cells[2].innerHTML!=='男'){
				meninput[i-1].checked = checkAllIDwomen.checked;
				}
			}
			checkAllID.checked=false;
			checkAllIDmen.checked=false;
		}
	</script>
</html>
